<?
include("includes/functions.php");
include("includes/sajax/sajaxResources.php");
include("includes/sajax/sajax.php");

sajax_init();
sajax_export("getResources");
sajax_export("deleteUploadedLinks");
sajax_handle_client_request();

function deleteUploadedLinks($uploadCheck) {
	global $dbname, $db, $db_selected;
	$sql = "SELECT from transLinks WHERE UploadCheck = '$uploadCheck'";
	//$result = mysql_db_query($dbname,$sql);
}

$fileDir = $rootDir.'admin/files/';

$someFile     = $_FILES['image']['tmp_name'];
$someFileName = $_FILES['image']['name'];
$someFileType = $_FILES['image']['type'];
$someFileSize = $_FILES['image']['size'];

$filename = stripslashes($someFileName);
$filename = cleanFilename($filename);	

// Determine mime type
if ($_POST[uploadConfirm] == 1) {
	if (
		($someFileType == "text/plain")               ||
		($someFileType == "application/vnd.ms-excel") ||
		($someFileType == "application/excel")        ||
		($someFileType == "application/octet-stream")
		) {
		$csv = 1;
		$filePath = $fileDir.$filename;
	} else { 
		$errorCSV     = 1; 
		$errorMessage = 'This doesn\'t look much like a CSV file. Could you please try again.  Thanks.'; 
	}
	
	if ($errorCSV != 1) {
		if(move_uploaded_file($someFile, $filePath)){					
			// Do our CSV processing here
			$csvFile = 'files/'.$filename;
			parse_csv_file($csvFile);
			$totalRows = count($rows);
			
			// Check for heading row
			if ( strlen($rows[0][0])>1) {
				$start = 1;
			} else {
				$start = 0;
			}
			
			// Get random string for UploadCheck
			$uploadCheck = random_string(10);
			$i=0;
			for($a=$start;$a<=($totalRows-1);$a++) {
				$path        = '/'.$rows[$a][0].'/'.$rows[$a][1].'/'.$rows[$a][2];
				$title       = fieldToDB($rows[$a][7]);
				$description = fieldToDB($rows[$a][8]);
				$link        = fieldToDB($rows[$a][9]); 
				$userID      = 20;
				
				if ( !empty($path) && !empty($title) && !empty($link) ) { 
				
					// Add record to database
					$sql = "INSERT into transLinks (
							Path,
							Title,
							LinkDescription,
							Link,
							UserID,
							DateCreated,
							Status,
							UploadCheck
							) values (
							'$path',
							'$title',
							'$description',
							'$link',
							'20',
							'".date('Y-m-d')."',
							'1',
							'$uploadCheck'
							)";
					$result = mysql_db_query($dbname,$sql);		
					$i++;
				}			
			}
		}	
	}	
}	
?>

<html><head>
<script language="JavaScript" type="text/javascript">

</script>
<script language="javascript">
var par = window.parent.document;
var progressDiv = par.getElementById('uploadStatusBatch');
var newResourceDisplay = par.getElementById('resourceList');

<? if ( ($_POST[uploadConfirm] == 1) && ($errorCSV != 1) ) { ?>
	<? sajax_show_javascript(); ?>
	
	function returnUploadUndo() {
		progressDiv.innerHTML = 'Your uploaded links have been deleted.';
	}
	function deleteUploadedLinks() {
		x_deleteUploadedLinks('<?=$uploadCheck; ?>',returnUploadUndo);
	}
	x_getResources(0,4,'<?=$uploadCheck; ?>',returnGetResources);
		
	function returnGetResources(string) {
		var stringArray = string.split('|');
		var levelString = stringArray[0];
		newResourceDisplay.innerHTML = levelString;
	}
	var successContent = '<div class="successMessage"><?=$i; ?> links uploaded successfully.</div>';
	//successContent    += '<div style="cursor: pointer;" onClick="deleteUploadedLinks(); return false;"><b>Oops! Undo.</b></div>';
	progressDiv.innerHTML = successContent;
	
<? } elseif ($errorCSV == 1) { ?>
	progressDiv.innerHTML = '<div class="errorMessage"><?=$errorMessage; ?></div>';
<? } ?>
function upload(){
	var par = window.parent.document;
	var iframe = par.getElementById('uploadBatch');
			
	var progressDiv = par.getElementById('uploadStatusBatch');
	progressDiv.innerHTML = '<img src="images/ajaxAnimBar.gif">';
		
	// send
	document.iform.submit();	
}
</script>
<head>
<style>
body {
	background: #fff;
}
td,p,table,tr,input,select,textarea {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 68.75%;
}
</style>
<body>
<form name="iform" method="post" enctype="multipart/form-data">
<input type="hidden" name="uploadConfirm" value="1">
<input id="fileCSV" type="file" name="image" size="30" style="font-size: 10px;" />
<div style="padding-top: 3px;"><input type="submit" onClick="upload();" value="Upload"></div>
</form>
</html>